<template>
  <div class="orderWrapper">
    <div class="handle-box">
      <div>
        <el-date-picker
          v-model="timeVal"
          type="daterange"
          range-separator="至"
          start-placeholder="开团开始日期"
          end-placeholder="开团结束日期">
        </el-date-picker>

        <el-input v-model="paramObj.managerName" placeholder="团长名称" class="handle-input mr10"></el-input>
        <el-input v-model="paramObj.productName" placeholder="商品名称" class="handle-input mr10"></el-input>
        <el-select v-model="paramObj.deliveryStatus" placeholder="选择订单状态">
          <el-option
            v-for="item in typeList"
            :key="item.name"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
        <el-input v-model="paramObj.orderId" placeholder="订单号" class="handle-input mr10"></el-input>
      </div>

      <div style="margin-top: 10px">
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        <el-button type="primary" icon="el-icon-search" @click="exportExcel">导出</el-button>
      </div>
      <!-- <el-button type="primary" icon="edit" @click="create()" class="create-btn">新建</el-button> -->
    </div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="订单号" align="center"></el-table-column>
      <el-table-column prop="promo.startTimestamp" label="开团时间" align="center" :formatter="dateFormat"></el-table-column>
      <el-table-column prop="manager.name" label="团长" align="center"></el-table-column>
      <el-table-column prop="orderAmount" label="支付金额" align="center"></el-table-column>
      <el-table-column prop="payStatus" label="支付状态" align="center" :formatter="payFormat"></el-table-column>
      <el-table-column prop="orderStatus" label="订单状态" align="center" :formatter="orderStatusFormat"></el-table-column>
      <el-table-column prop="deliveryStatus" label="发货状态" align="center" :formatter="deliveryStatusFormat"></el-table-column>
      <el-table-column label="操作" align="center" width="400">
        <template slot-scope="scope">
          <el-button size="small" @click="handleView(scope.$index, scope.row)">查看</el-button>
          <el-button size="small" type="danger" @click="handleRefund(scope.$index, scope.row)" v-if="scope.row.orderStatus==0">退款</el-button>
          <el-button size="small" type="danger" @click="handleDelivery(scope.$index, scope.row)" v-if="scope.row.deliveryStatus==0 && scope.row.orderStatus==0">发货</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount" :page-size="paramObj.limit" @current-change="handleCurrentChange">
    </el-pagination>
    
  </div>

</template>

<script>
import api from "@/api/order";
import { formatDate } from "@/utils/date";

const SUCCESS = 0;
const LIMIT = 10;
export default {
  data() {
    return {
      timeVal: "",
      tableData: [],
      totalCount: 0,
      optId: "",
      paramObj: {
        startTimestamp: null,
        endTimestamp: null,
        managerName: null,
        productName: null,
        deliveryStatus: null,
        orderId: null,
        currentPage: 1,
        limit: LIMIT
      },
      typeList: [
        {
          name: "选择发货状态",
          value: null
        },
        {
          name: "未发货",
          value: 0
        },
        {
          name: "已发货",
          value: 1
        }
      ]
    };
  },
  created() {
    this._getData();
  },
  methods: {
    deliveryStatusFormat(row, column) {
      let deliveryStatus = row[column.property];
      if (deliveryStatus == 0) {
        return "未发货";
      } else {
        return "已发货";
      }
    },
    orderStatusFormat(row, column) {
      let orderStatus = row[column.property];
      if (orderStatus == 0) {
        return "已创建";
      }
      if (orderStatus == 1) {
        return "已取消";
      }
      if (orderStatus == 3) {
        return "待退款";
      }
      if (orderStatus == 4) {
        return "已退款";
      }
    },
    payFormat(row, column) {
      let payStatus = row[column.property];
      if (payStatus == 0) {
        return "未支付";
      } else {
        return "已支付";
      }
    },
    dateFormat(row, column) {
      if (row.promo) {
        let date = row.promo.startTimestamp;
        if (date == undefined) {
          return "";
        }
        date = new Date(date);
        let format = "yyyy-MM-dd hh:mm:ss";
        return formatDate(date, format);
      }
    },
    handleView(index, row) {
      this.$router.push({ path: "/order/detail", query: { id: row.id } });
    },
    handleCurrentChange(val) {
      this.paramObj.currentPage = val;
      this._getData();
    },
    handleRefund(index, row) {
      this.optId = row.id;
      this.$confirm("确定退款?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._sureRefund();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    _sureRefund() {
      api.refund(this.optId).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }
      });
    },
    handleDelivery(index, row) {
      this.optId = row.id;
      this.$confirm("确定发货?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._sureDelivery();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    _sureDelivery() {
      api.deliverProduct(this.optId).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }
      });
    },
    _getData() {
      api.getList(this.paramObj).then(response => {
        if (response.code == SUCCESS) {
          this.tableData = response.data.orderList;
          this.totalCount = response.data.totalCount;
        }
      });
    },
    create() {
      let order = {
        openid: "aaaaabbbbbcccccdddddeeeeewww",
        originAmount: 200,
        orderAmount: 150,
        managerId: 26,
        promoId: 129,
        name: "范骏",
        nickname: "fan",
        phone: "18702121893",
        pickAddress: "上海市嘉定区江桥万达",
        remarks: "这是备注啊",
        orderProductList: [
          {
            productId: 302,
            productNumber: 2
          },
           {
            productId: 306,
            productNumber: 1
          }
        ]
      };

      api.createOrder(order).then(response => {
        if (response.code == SUCCESS) {
        }
      });
    },
    search() {
      this.handleParams();

      let result = this.verifyOneParam();
      if (!result) {
        this.$message({
          type: "warning",
          message: "参数只能选择一个"
        });
        return;
      }

      this.paramObj.currentPage = 1;
      this._getData();
    },
    exportExcel() {
      this.handleParams();

      let url = "/api/export/order?";
      if (this.paramObj.startTimestamp && this.paramObj.endTimestamp) {
        url =
          url +
          "startTimestamp=" +
          this.paramObj.startTimestamp +
          "&endTimestamp=" +
          this.paramObj.endTimestamp;
      }

      if (this.paramObj.managerName) {
        url = url + "managerName=" + this.paramObj.managerName;
      }

      if (this.paramObj.productName) {
        url = url + "productName=" + this.paramObj.productName;
      }

      if (this.paramObj.deliveryStatus) {
        url = url + "deliveryStatus=" + this.paramObj.deliveryStatus;
      }

      if (this.paramObj.orderId) {
        url = url + "orderId=" + this.paramObj.orderId;
      }

      window.location.href = url;
    },
    verifyOneParam() {
      let paramNumber = 0;
      if (this.paramObj.startTimestamp && this.paramObj.endTimestamp) {
        paramNumber += 1;
      }
      if (this.paramObj.managerName) {
        paramNumber += 1;
      }

      if (this.paramObj.productName) {
        paramNumber += 1;
      }

      if (this.paramObj.deliveryStatus) {
        paramNumber += 1;
      }

      if (this.paramObj.orderId) {
        paramNumber += 1;
      }

      if (paramNumber > 1) {
        return false;
      }
      return true;
    },
    handleParams() {
      if (this.timeVal) {
        const format = "yyyy-MM-dd";
        let startTimeStr =
          formatDate(new Date(this.timeVal[0]), format) + " 00:00:00";
        let endTimeStr =
          formatDate(new Date(this.timeVal[1]), format) + " 23:59:59";
        console.log(startTimeStr);
        console.log(endTimeStr);

        this.paramObj.startTimestamp = new Date(startTimeStr).getTime();
        this.paramObj.endTimestamp = new Date(endTimeStr).getTime();
      } else {
        this.paramObj.startTimestamp = null;
        this.paramObj.endTimestamp = null;
      }

      if (this.paramObj.managerName == "") {
        this.paramObj.managerName = null;
      }
      if (this.paramObj.productName == "") {
        this.paramObj.productName = null;
      }
      if (this.paramObj.orderId == "") {
        this.paramObj.orderId = null;
      }
    }
  }
};
</script>

<style lang="less">
.orderWrapper {
  .handle-box {
    margin-bottom: 20px;
    .handle-select {
      width: 240px;
    }
    .handle-input {
      width: 200px;
      display: inline-block;
    }
    .create-btn {
      float: right;
      margin-right: 20px;
    }
  }
}
</style>